package com.wagn.eduservice.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wagn.eduservice.entity.teacher.QueryTeacher;
import com.wagn.eduservice.service.EduTeacherService;
import com.wagn.eduservice.entity.EduTeacher;
import com.wagn.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-09
 */
@Api(description="教师管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;


    //增加老师
    @ApiOperation("添加老师")
    @PostMapping("addTeacher")
    public R add(@RequestBody EduTeacher eduTeacher){
        if(eduTeacherService.save(eduTeacher)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //删除一个老师（逻辑删除）
    @ApiOperation("根据id逻辑删除一个老师")
    @DeleteMapping("{id}")
    public R deleteById(@ApiParam(name = "id",value = "教师ID") @PathVariable String id){
        boolean rs = eduTeacherService.removeById(id);
        R r;
        if(rs){
            r = R.ok();
        }else{
            r = R.error();
        }
        return r;
    }

    //通过ID修改老师
    @ApiOperation("通过ID修改老师")
    @PostMapping("updateTeacher")
    public R getTeacherById(@RequestBody EduTeacher eduTeacher){
        if(eduTeacherService.updateById(eduTeacher)){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //通过ID查询老师
    @ApiOperation("通过ID查询老师")
    @GetMapping("{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    //查询所有老师
    @ApiOperation("查询所有老师")
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> rs = eduTeacherService.list(null);
        return R.ok().data("items",rs);
    }





    //分页查询
    @ApiOperation("分页查询老师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> page = new Page<>(current,limit);
        eduTeacherService.page(page,null);
        long total = page.getTotal();
        List<EduTeacher> rows = page.getRecords();
        return R.ok().data("total",total).data("rows",rows);
    }

    //多条件分页查询
    @ApiOperation("多条件分页查询老师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R QueryPageTeacher(@PathVariable long current,
                              @PathVariable long limit,
                              @RequestBody(required = false) QueryTeacher queryTeacher){
        //初始化Page
        Page<EduTeacher> page = new Page<>(current, limit);

        //ArrayList arrayList = new ArrayList();

        //添加条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = queryTeacher.getName();
        Integer level = queryTeacher.getLevel();
        String start = queryTeacher.getStart();
        String end = queryTeacher.getEnd();

        //模糊查询
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        //等于
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        //大于等于
        if(!StringUtils.isEmpty(start)){
            wrapper.ge("gmt_create",start);
        }
        //小于等于
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");


        //执行，结果封装在Page中
        eduTeacherService.page(page,wrapper);

        long total = page.getTotal();
        List<EduTeacher> rows = page.getRecords();
        return R.ok().data("total",total).data("rows",rows);
    }

}

